import json
import pickle

from pythonds.bltree import btree, insert_left, insert_right, get_left_child, layer_order, layer_count
from pythonds.trees import *


def test():
    test_btree_save()


def test_BTree_save():
    et = BTree("hello")
    with open("p1.pkl", "wb") as p1:
        pickle.dump(et, p1)

    et.insert_left("word")
    et.insert_left("haha")
    et.insert_right("python")
    with open("p2.pkl", "wb") as p2:
        pickle.dump(et, p2)


def test_BTree_load():
    with open("p1.pkl", "rb") as f:
        p1: BTree = pickle.load(f)
        p1.layer_order()

    print("-" * 15)

    with open("p2.pkl", "rb") as f2:
        p2: BTree = pickle.load(f2)
        p2.layer_order()


def test_btree_save():
    ltree = btree("hello")
    insert_left(ltree, "word")
    insert_right(ltree, "python")
    insert_right(ltree, "cn")
    insert_left(get_left_child(ltree), "vscode")
    insert_right(get_left_child(ltree), "pycharm")
    layer_order(ltree)
    print("-" * 15)
    print(layer_count(ltree))
    print(json.dumps(ltree))
    with open("p3.pkl", "wb") as f:
        pickle.dump(ltree, f)

    with open("p3.pkl", "rb") as f:
        tree = pickle.load(f)
        layer_order(tree)
